package com.ihrm.domain.system.response;

import com.ihrm.domain.system.Permission;
import com.ihrm.domain.system.Role;
import com.ihrm.domain.system.User;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

@Setter
@Getter
public class ProfileResult {
    private String mobile;
    private String username;
    private String company;
    private Map<String,Object> roles = new HashMap<>();

    public ProfileResult(User user,List<Permission> list){
        this.mobile = user.getMobile();
        this.username = user.getUsername();
        this.company = user.getCompanyName();

        Set<String> menus = new HashSet<>();
        Set<String> points = new HashSet<>();
        Set<String> apis = new HashSet<>();

        for (Permission perm : list) {
            String code = perm.getCode();
            if(perm.getType() == 1){
                menus.add(code);
            }else if(perm.getType() == 1){
                points.add(code);
            }else{
                apis.add(code);
            }
        }

        this.roles.put("menus",menus);
        this.roles.put("points",points);
        this.roles.put("apis",apis);
    }

    public ProfileResult(User user){
        this.mobile = user.getMobile();
        this.username = user.getUsername();
        this.company = user.getCompanyName();

        Set<Role> roles = user.getRoles();

        Set<String> menus = new HashSet<String>();
        Set<String> points = new HashSet<String>();
        Set<String> api = new HashSet<String>();

        for (Role role : roles) {
            for (Permission permission : role.getPermissions()) {
                int type = permission.getType();
                String code = permission.getCode();
                switch (type){
                    case 1:
                        menus.add(code);
                        break;
                    case 2:
                        points.add(code);
                        break;
                    case 3:
                        api.add(code);
                }
            }
        }
        Map<String,Object> rolesmap = new HashMap<String,Object>();
        rolesmap.put("menus",menus);
        rolesmap.put("points",points);
        rolesmap.put("api",api);
        this.roles = rolesmap;
    }
}
